package edu.guet.server.controller;

import com.fasterxml.jackson.databind.jsonschema.JsonSerializableSchema;
import edu.guet.server.model.SemesterStatistic.*;
import edu.guet.server.model.dto.UnitDTO;
import edu.guet.server.service.*;
import edu.guet.server.utils.ResultAPI;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/course")
public class SemesterStatisticController {
    @Autowired
    SemesterService semesterService;
    @Autowired
    StatisticService statisticService;
    @Autowired
    UnitService unitService;
    @Autowired
    private TestService testService;
    @Autowired
    private StudentHomeworkService studentHomeworkService;

    @GetMapping("/stats")
    public ResultAPI stats(@RequestParam Integer semester_id) {
        SemesterTask semesterTask = semesterService.findSemesterTask(semester_id);
        return ResultAPI.success(semesterTask);
    }

    /**
     * 选课人数图表数据接口
     *
     * @param semester_id
     * @return
     */
    @GetMapping("/enrollment-chart")
    public ResultAPI enrollmentChart(@RequestParam Integer semester_id) {
        log.info("/enrollment-chart:{}", semester_id);
        ChartCount chartCount = statisticService.selectSemCount(semester_id);
        return ResultAPI.success(chartCount);
    }

    /**
     * 视频观看人数图表数据接口
     *
     * @param semester_id
     * @return
     */
    @GetMapping("/video-viewers-chart")
    public ResultAPI videoViewersChart(@RequestParam Integer semester_id) {
        log.info("/video-viewers-chart:{}", semester_id);
        ChartCount chartCount = statisticService.findVideoCount(semester_id);
        return ResultAPI.success(chartCount);
    }

    /**
     * 获取章节名称和id列表
     *
     * @param semester_id
     * @return
     */
    @GetMapping("/getUnits")
    public ResultAPI getUnit(@RequestParam Integer semester_id) {
        log.info("/getUnit:{}", semester_id);
        List<UnitDTO> units = unitService.findUnits(semester_id);
        log.info("/getUnits:{}", units);
        return ResultAPI.success(units);
    }

    /**
     * 课时/测验/作业 (Course Schedule)
     *
     * @param unit_id
     * @return
     */
    @GetMapping("/schedule")
    public ResultAPI getSchedule(@RequestParam Integer unit_id) {
        log.info("/schedule:{}", unit_id);
        ChapterScores chapterScores = new ChapterScores();
        String chapterName = unitService.findUnitName(unit_id);
        List<ChapterScores.TestScore> testScore = testService.findTestScore(unit_id);
        List<ChapterScores.HomeworkScore> homeworkScores = testService.findHmkScore(unit_id);
        chapterScores.setTestScores(testScore);
        chapterScores.setHomeworkScores(homeworkScores);
        chapterScores.setChapter(chapterName);
        return ResultAPI.success(testScore);
    }

    /**
     * 测验成绩图表数据接口
     *
     * @param unit_id
     * @return
     */
    @GetMapping("/test-scores")
    public ResultAPI getTestScores(@RequestParam Integer unit_id) {
        log.info("/test-scores:{}", unit_id);
        ChartCount chartCount = null;
        try {
            chartCount = statisticService.findUnitTestScoreCounts(unit_id);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultAPI.error("数据库查询错误！");
        }
        log.info("/test-scores:{}", chartCount);
        return ResultAPI.success(chartCount);
    }

    /**
     * 作业成绩图表数据接口
     *
     * @param unit_id
     * @return
     */
    @GetMapping("/homework-scores")
    public ResultAPI getHomeworkScores(@RequestParam Integer unit_id) {
        log.info("/homework-scores:{}", unit_id);
        ChartCount chartCount = statisticService.findUnitHmkScoreCounts(unit_id);
        return ResultAPI.success(chartCount);
    }

    /**
     * 权重设置接口
     *
     * @param semesterPermissions
     * @return
     */
    @PostMapping("/permissions")
    public ResultAPI permissions(@RequestBody SemesterPermissions semesterPermissions) {
        statisticService.UpdateValidScore(semesterPermissions);
        return ResultAPI.success();
    }

    /**
     * 成绩分布图表数据接口
     *
     * @param semester_id
     * @return
     */
    @GetMapping("/grades-distribution-chart")
    public ResultAPI getGradeDistributionChart(@RequestParam Integer semester_id) {
        log.info("/grades-distribution-chart:{}", semester_id);
        ChartCount chartCount = statisticService.getValidScores(semester_id);
        return ResultAPI.success(chartCount);
    }

    /**
     * @param semester_id
     * @return
     */
    @GetMapping("/exam-pass-chart")
    public ResultAPI getExamPassChart(@RequestParam Integer semester_id) {
        log.info("/exam-pass-chart:{}", semester_id);
        ChartCount chartCount = statisticService.getPassValidScores(semester_id);
        return ResultAPI.success(chartCount);
    }

    @GetMapping("/grades")
    public ResultAPI getGrades(@RequestParam Integer semester_id) {
        log.info("/grades:{}", semester_id);
        ValidScore validScore = statisticService.getALLValidScore(semester_id);
        return ResultAPI.success(validScore);
    }

    @GetMapping("/learningData")
    public ResultAPI getLearningData(@RequestParam Integer semester_id) {
        log.info("/learningData:{}", semester_id);
        List<Student> studentList = statisticService.getlearningData(semester_id);
        return ResultAPI.success(studentList);
    }

    @GetMapping("/discussion")
    public ResultAPI getDiscussion(@RequestParam Integer semester_id) {
        log.info("/discussion:{}", semester_id);
        List<Discussion> list = statisticService.getDiscussionData(semester_id);
        return ResultAPI.success(list);
    }
}
